#include "debug.h"
#include "../DDIC/AllDDIC.h"
#include "../DEMURA/Demura.h"
#include "usbd_cdc.h"

//----------------video mode use--------------------------
u8 NT37701A_bufferR[70];
u8 NT37701A_bufferG[70];
u8 NT37701A_bufferB[70];
//----------------command mode use------------------------
u8 NT37701A_REG_B0[26]; //length+cmd+data
u8 NT37701A_REG_B1[26];
u8 NT37701A_REG_B2[24];

u8 NT37701A_REG_B3[26];
u8 NT37701A_REG_B4[26];
u8 NT37701A_REG_B5[24];

u8 NT37701A_REG_B6[26];
u8 NT37701A_REG_B7[26];
u8 NT37701A_REG_B8[24];
//---------------------------------------

void NT37701A_Inter_BIST(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{	
	//To selece Gray0 pattern in bist mode
	//CMD page0
	u8 PAGE0[7]={0x06,0xf0,0x55,0xaa,0x52,0x08,0x00};
	u8 cmd1[6]={0x05,0xee,0x87,0x78,0x02,0x40};
	u8 cmd2[9]={0x08,0xef,0x00,0x01,0xff,0xff,0xff,0x1f,0xff};	
	SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0410)&0XFFFD);
	delay_ms(2);	
	
	SSD2828_W_Array(SigMode,channel,PAGE0,0);
	SSD2828_W_Array(SigMode,channel,cmd1,0);
	SSD2828_W_Array(SigMode,channel,cmd2,0);
	delay_ms(5);
	STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+3);   
}

void NT37701A_Write_Register(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
	  SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0010)&0XFFfd);
    SSD2828_W_Array(SigMode,channel,buffer,2);                               

    delay_ms(5);
    SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
    buffer[4] = Uart_Error_None;
    buffer[5] = 0;
    STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //返回 写寄存器状态  ：ok  
}
void NT37701A_Read_Register(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
    u16 tmp;
    buffer[1]=0x03;
    SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data|0x0080);                               
    SSD2828_W_Reg(SigMode,channel,0xC1,buffer[2]); //return package size                                
    SSD2828_W_Reg(SigMode,channel,0xC0,0x0001);
    buffer[0] = 0x01;
    buffer[1] = buffer[3];                        
    SSD2828_W_Array(SigMode,channel,buffer,0);
    delay_ms(5);  
    SSD2828_W_Reg(SigMode,channel,0xD4,0x00FA);
    SSD2828_W_Cmd(SigMode,channel,0xFF);
    for(i=0;i<buffer[2]-1;i++)
    {
        SSD2828_W_Cmd(SigMode,channel,0xFA);		
        tmp=SPI3_Read_u16_Data(channel);
        buffer[4+i]=tmp>>8;
        buffer[5+i]=tmp;
        delay_ms(5);
        i++;
    }
		buffer[0]=NT37701A;	
		buffer[1]=0x02;
    //SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
		SSD2828_W_Reg(SigMode,channel,0xB7,0x034b);
    STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //返回 读取的 寄存器数据  ：ok 
}



void NT37701A_Video_Write_Gamma(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
    u8 buffer1[7];
    u8 i,j,k=0,temp;
	  u8 m = 0x00;
		u8 PAGE2[7]={0x06,0xf0,0x55,0xaa,0x52,0x08,0x02};	
		
		if((buffer[3]&0xFF)==0x00)  //R
		{
			for( i = 0;i<18;i++)   //12个绑点                                                                                  
			{NT37701A_bufferR[i]=buffer[i+5];}	    
			for( i = 0;i<18;i++)   //12个绑点                                                                                 
			{NT37701A_bufferR[i+18]=buffer[i+5+18];}
			for( i = 0;i<16;i++)   //10个绑点	                                                                                  
			{NT37701A_bufferR[i+36]=buffer[i+5+36];}
	  }
		else if((buffer[3]&0xFF)==0x42)  //G
		{
			for( i = 0;i<18;i++)   //12个绑点                                                                                  
			{NT37701A_bufferG[i]=buffer[i+5];}	    
			for( i = 0;i<18;i++)   //12个绑点                                                                                 
			{NT37701A_bufferG[i+18]=buffer[i+5+18];}
			for( i = 0;i<16;i++)   //10个绑点	                                                                                  
			{NT37701A_bufferG[i+36]=buffer[i+5+36];}
		}
		else if((buffer[3]&0xFF)==0x81)  //B
		{
			for( i = 0;i<18;i++)   //12个绑点                                                                                  
			{NT37701A_bufferB[i]=buffer[i+5];}	    
			for( i = 0;i<18;i++)   //12个绑点                                                                                 
			{NT37701A_bufferB[i+18]=buffer[i+5+18];}
			for( i = 0;i<16;i++)   //10个绑点	                                                                                  
			{NT37701A_bufferB[i+36]=buffer[i+5+36];}
	  }

		SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0410)&0XFFFD);
		delay_ms(2);	
		
		if(buffer[4]==0x40||buffer[4]==0x41||buffer[4]==0x42) //aod gamma group;
		{
//			buffer1[0] = 0x02; 
//			buffer1[1] = 0x2F;
//			buffer1[1] = 0x01;
//			SSD2828_W_Array(SigMode,channel,buffer1,0);		
//			buffer1[0] = 0x01; 
//			buffer1[1] = 0x39;
//			SSD2828_W_Array(SigMode,channel,buffer1,0);		
//			buffer1[0] = 0x01; 
//			buffer1[1] = 0x2c;
//			SSD2828_W_Array(SigMode,channel,buffer1,0);			
		}
		else
		{
			buffer1[0] = 0x01; 
			buffer1[1] = 0x38;
			SSD2828_W_Array(SigMode,channel,buffer1,0);				
		}
		SSD2828_W_Array(SigMode,channel,PAGE2,0);	
		delay_ms(2);	
//	  buffer1[0] = 0x02;        
//    buffer1[1] = 0xCE;
//    buffer1[2] = 0x00;
//    SSD2828_W_Array(SigMode,channel,buffer1,0);		
		
	  buffer1[0] = 0x02;        
    buffer1[1] = 0xCC;
    buffer1[2] = 0x10;
    SSD2828_W_Array(SigMode,channel,buffer1,0);	
		delay_ms(1); 		
	  buffer1[0] = 0x02; 
		buffer1[1] = 0xBF;
		buffer1[2] = buffer[4]; //gamma_group_sel
		SSD2828_W_Array(SigMode,channel,buffer1,0);
		delay_ms(1); 		
		SSD2828_W_Reg(SigMode,channel,0xBC,19); 
		SSD2828_W_Cmd(SigMode,channel,0xBF);	
		SSD2828_W_Data(SigMode,channel,0xB0); //GAMMA RED GROUP1
		for(i=0; i<18; i++)
		{
			SSD2828_W_Data(SigMode,channel,NT37701A_bufferR[i]);	
		}				 
		
		SSD2828_W_Reg(SigMode,channel,0xBC,19); 
		SSD2828_W_Cmd(SigMode,channel,0xBF);	
		SSD2828_W_Data(SigMode,channel,0xB1); //GAMMA RED GROUP2
		for(i=0; i<18; i++)
		{
			SSD2828_W_Data(SigMode,channel,NT37701A_bufferR[i+18]);	
		}				
		SSD2828_W_Reg(SigMode,channel,0xBC,17); 
		SSD2828_W_Cmd(SigMode,channel,0xBF);	
		SSD2828_W_Data(SigMode,channel,0xB2); //GAMMA RED GROUP3
		for(i=0; i<16; i++)
		{
			SSD2828_W_Data(SigMode,channel,NT37701A_bufferR[i+36]);	
		}		
		delay_ms(2); 		
//---------------------------------------------------------------------		
		SSD2828_W_Reg(SigMode,channel,0xBC,19); 
		SSD2828_W_Cmd(SigMode,channel,0xBF);	
		SSD2828_W_Data(SigMode,channel,0xB3); //GAMMA RED GROUP1
		for(i=0; i<18; i++)
		{
			SSD2828_W_Data(SigMode,channel,NT37701A_bufferG[i]);	
		}				 
		
		SSD2828_W_Reg(SigMode,channel,0xBC,19); 
		SSD2828_W_Cmd(SigMode,channel,0xBF);	
		SSD2828_W_Data(SigMode,channel,0xB4); //GAMMA RED GROUP2
		for(i=0; i<18; i++)
		{
			SSD2828_W_Data(SigMode,channel,NT37701A_bufferG[i+18]);	
		}									 
		SSD2828_W_Reg(SigMode,channel,0xBC,17); 
		SSD2828_W_Cmd(SigMode,channel,0xBF);	
		SSD2828_W_Data(SigMode,channel,0xB5); //GAMMA RED GROUP3
		for(i=0; i<16; i++)
		{
			SSD2828_W_Data(SigMode,channel,NT37701A_bufferG[i+36]);	
		}
		delay_ms(2); 		
//----------------------------------------------------------------------		
		SSD2828_W_Reg(SigMode,channel,0xBC,19); 
		SSD2828_W_Cmd(SigMode,channel,0xBF);	
		SSD2828_W_Data(SigMode,channel,0xB6); //GAMMA BLUE GROUP1
		for(i=0; i<18; i++)
		{
			SSD2828_W_Data(SigMode,channel,NT37701A_bufferB[i]);	
		}				 	
		SSD2828_W_Reg(SigMode,channel,0xBC,19); 
		SSD2828_W_Cmd(SigMode,channel,0xBF);	
		SSD2828_W_Data(SigMode,channel,0xB7); //GAMMA BLUE GROUP2
		for(i=0; i<18; i++)
		{
			SSD2828_W_Data(SigMode,channel,NT37701A_bufferB[i+18]);	
		}									 
		SSD2828_W_Reg(SigMode,channel,0xBC,17); 
		SSD2828_W_Cmd(SigMode,channel,0xBF);	
		SSD2828_W_Data(SigMode,channel,0xB8); //GAMMA BLUE GROUP3
		for(i=0; i<16; i++)
		{
			SSD2828_W_Data(SigMode,channel,NT37701A_bufferB[i+36]);	
		}
		delay_ms(2); 	
	//---------------------------------------------------------		
//	  buffer1[0] = 0x02;        
//    buffer1[1] = 0xCC;
//    buffer1[2] = 0x30;
//    SSD2828_W_Array(SigMode,channel,buffer1,0);		
//		delay_ms(1); 		
//	  buffer1[0] = 0x02;        
//    buffer1[1] = 0xCE;
//    buffer1[2] = 0x01;
//    SSD2828_W_Array(SigMode,channel,buffer1,0);		
//		delay_ms(5); 	
			if(SigMode == CMD_Mode)
			{
				buffer1[0] = 0x01;        
				buffer1[1] = 0x2C;
				SSD2828_W_Array(SigMode,channel,buffer1,0);		
			}

		SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);				
		delay_ms(2); 
    buffer[0]=NT37701A;	//NT37701A
    buffer[1]=0x08;		
    buffer[4] = Uart_Error_None;
    buffer[5] = 0;
    STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+3);            //返回 写寄存器状态  ：ok  
}


void NT37701A_Command_Write_Gamma(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
    u8 buffer1[7];
    u8 i,j,k=0,temp;
	  u8 m = 0x00;
		u8 PAGE2[7]={0x06,0xf0,0x55,0xaa,0x52,0x08,0x02};	

		if((buffer[3]&0xFF)==0x00)  //R
		{ 
			NT37701A_REG_B0[0]=19;  //length=cmd+data
			NT37701A_REG_B0[1]=0xB0;
			for( i = 0;i<18;i++)   //9个绑点                                                                                  
			{
				NT37701A_REG_B0[i+2]=buffer[i+5];
			}	  
			NT37701A_REG_B1[0]=19;
			NT37701A_REG_B1[1]=0xB1;	
			for( i = 0;i<18;i++)   //9个绑点                                                                                 
			{
				NT37701A_REG_B1[i+2]=buffer[i+5+18];
			}
			NT37701A_REG_B2[0]=15;
			NT37701A_REG_B2[1]=0xB2;	
			for( i = 0;i<16;i++)   //7个绑点	                                                                                  
			{
				NT37701A_REG_B2[i+2]=buffer[i+5+36];
			}
	  }
		else if((buffer[3]&0xFF)==0x42)  //G
		{
			NT37701A_REG_B3[0]=19;  //length=cmd+data
			NT37701A_REG_B3[1]=0xB3;
			for( i = 0;i<18;i++)   //12个绑点                                                                                  
			{
				NT37701A_REG_B3[i+2]=buffer[i+5];
			}
			NT37701A_REG_B4[0]=19; 
			NT37701A_REG_B4[1]=0xB4;	
			for( i = 0;i<18;i++)   //12个绑点                                                                                 
			{
				NT37701A_REG_B4[i+2]=buffer[i+5+18];
			}
			NT37701A_REG_B5[0]=15; 
			NT37701A_REG_B5[1]=0xB5;	
			for( i = 0;i<16;i++)   //10个绑点	                                                                                  
			{
				NT37701A_REG_B5[i+2]=buffer[i+5+36];
			}
		}
		else if((buffer[3]&0xFF)==0x81)  //B
		{
			NT37701A_REG_B6[0]=19;  //length=cmd+data
			NT37701A_REG_B6[1]=0xB6;
			for( i = 0;i<18;i++)   //12个绑点                                                                                  
			{
				NT37701A_REG_B6[i+2]=buffer[i+5];
			}	
			NT37701A_REG_B7[0]=19;
			NT37701A_REG_B7[1]=0xB7;
			for( i = 0;i<18;i++)   //12个绑点                                                                                 
			{
				NT37701A_REG_B7[i+2]=buffer[i+5+18];
			}
			NT37701A_REG_B8[0]=19;
			NT37701A_REG_B8[1]=0xB8;
			for( i = 0;i<16;i++)   //10个绑点	                                                                                  
			{
				NT37701A_REG_B8[i+2]=buffer[i+5+36];
			}
	  }

		SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data); //LP mode only
		delay_ms(5);	
		
		if(buffer[4]==0x40||buffer[4]==0x41||buffer[4]==0x42) //aod gamma group;
		{
//			buffer1[0] = 0x02; 
//			buffer1[1] = 0x2F;
//			buffer1[1] = 0x01; //
//			SSD2828_W_Array(SigMode,channel,buffer1,0);		
			
//			buffer1[0] = 0x01; 
//			buffer1[1] = 0x39;
//			SSD2828_W_Array(SigMode,channel,buffer1,0);		
//			buffer1[0] = 0x01; 
//			buffer1[1] = 0x2C;
//			SSD2828_W_Array(SigMode,channel,buffer1,0);		
		}
		else
		{
			buffer1[0] = 0x01; 
			buffer1[1] = 0x38;
			SSD2828_W_Array(SigMode,channel,buffer1,0);		
			buffer1[0] = 0x01; 
			buffer1[1] = 0x2C;
			SSD2828_W_Array(SigMode,channel,buffer1,0);				
		}
		SSD2828_W_Array(SigMode,channel,PAGE2,0);	
		delay_ms(5);	
	  buffer1[0] = 0x02; 
		buffer1[1] = 0xBF;
		buffer1[2] = buffer[4]; //gamma_group_sel
		SSD2828_W_Array(SigMode,channel,buffer1,0);
		delay_ms(5); 		
	  buffer1[0] = 0x02;        
    buffer1[1] = 0xCC;
    buffer1[2] = 0x30;
    SSD2828_W_Array(SigMode,channel,buffer1,0);		
		delay_ms(5); 	
	  buffer1[0] = 0x02;        
    buffer1[1] = 0xCE;
    buffer1[2] = 0x00;
    SSD2828_W_Array(SigMode,channel,buffer1,0);	
		delay_ms(5); 
		//GAMMA RED GROUP1
		SSD2828_W_Array(SigMode,channel,NT37701A_REG_B0,0);
		//GAMMA RED GROUP2
		SSD2828_W_Array(SigMode,channel,NT37701A_REG_B1,0);
	  //GAMMA RED GROUP3
		SSD2828_W_Array(SigMode,channel,NT37701A_REG_B2,0);
//---------------------------------------------------------------------		
		//GAMMA RED GROUP1
		SSD2828_W_Array(SigMode,channel,NT37701A_REG_B3,0);
		//GAMMA RED GROUP2
		SSD2828_W_Array(SigMode,channel,NT37701A_REG_B4,0);
	  //GAMMA RED GROUP3
		SSD2828_W_Array(SigMode,channel,NT37701A_REG_B5,0);		
//----------------------------------------------------------------------		
		//GAMMA RED GROUP1
		SSD2828_W_Array(SigMode,channel,NT37701A_REG_B6,0);
		//GAMMA RED GROUP2
		SSD2828_W_Array(SigMode,channel,NT37701A_REG_B7,0);
	  //GAMMA RED GROUP3
		SSD2828_W_Array(SigMode,channel,NT37701A_REG_B8,0);	
	//---------------------------------------------------------		
	  buffer1[0] = 0x02;        
    buffer1[1] = 0xCE;
    buffer1[2] = 0x01;
    SSD2828_W_Array(SigMode,channel,buffer1,0);		
		delay_ms(2);  	

		SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);				
		delay_ms(2); 
    buffer[0]=NT37701A;	//NT37701A
    buffer[1]=0x08;		
    buffer[4] = Uart_Error_None;
    buffer[5] = 0;
    STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+3);            //返回 写寄存器状态  ：ok  
}

void NT37701A_Write_Gamma(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
		if(SigMode==Video_Mode)
		{
			NT37701A_Video_Write_Gamma(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data);
		}
		else if(SigMode==CMD_Mode)
		{
			NT37701A_Command_Write_Gamma(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data);
		}
}

void NT37701A_Gamma_Update(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
    u8 buffer1[7];
    u8 i,j,k=0,temp;
	  u8 m = 0x00;
		u8 PAGE2[7]={0x06,0xf0,0x55,0xaa,0x52,0x08,0x02};	
		
		SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0410)&0XFFFD);
		delay_ms(5);			
		SSD2828_W_Array(SigMode,channel,PAGE2,0);	
		delay_ms(2);	
	  buffer1[0] = 0x02; 
		buffer1[1] = 0xBF;
		buffer1[2] = buffer[4]; //gamma_group_sel
		SSD2828_W_Array(SigMode,channel,buffer1,0);		
	  buffer1[0] = 0x02;        
    buffer1[1] = 0xCC;
    buffer1[2] = 0x30;
    SSD2828_W_Array(SigMode,channel,buffer1,0);	
	  buffer1[0] = 0x02;        
    buffer1[1] = 0xCE;
    buffer1[2] = 0x00;
    SSD2828_W_Array(SigMode,channel,buffer1,0);		
		delay_ms(2);				
	
		SSD2828_W_Reg(SigMode,channel,0xBC,19); 
		SSD2828_W_Cmd(SigMode,channel,0xBF);	
		SSD2828_W_Data(SigMode,channel,0xB0); //GAMMA RED GROUP1
		for(i=0; i<18; i++)
		{
			SSD2828_W_Data(SigMode,channel,NT37701A_bufferR[i]);	
		}				 
		
		SSD2828_W_Reg(SigMode,channel,0xBC,19); 
		SSD2828_W_Cmd(SigMode,channel,0xBF);	
		SSD2828_W_Data(SigMode,channel,0xB1); //GAMMA RED GROUP2
		for(i=0; i<18; i++)
		{
			SSD2828_W_Data(SigMode,channel,NT37701A_bufferR[i+18]);	
		}				
		
		SSD2828_W_Reg(SigMode,channel,0xBC,15); 
		SSD2828_W_Cmd(SigMode,channel,0xBF);	
		SSD2828_W_Data(SigMode,channel,0xB2); //GAMMA RED GROUP3
		for(i=0; i<14; i++)
		{
			SSD2828_W_Data(SigMode,channel,NT37701A_bufferR[i+36]);	
		}		
		delay_ms(2); 		
//---------------------------------------------------------------------		
		SSD2828_W_Reg(SigMode,channel,0xBC,19); 
		SSD2828_W_Cmd(SigMode,channel,0xBF);	
		SSD2828_W_Data(SigMode,channel,0xB3); //GAMMA RED GROUP1
		for(i=0; i<18; i++)
		{
			SSD2828_W_Data(SigMode,channel,NT37701A_bufferG[i]);	
		}				 
		
		SSD2828_W_Reg(SigMode,channel,0xBC,19); 
		SSD2828_W_Cmd(SigMode,channel,0xBF);	
		SSD2828_W_Data(SigMode,channel,0xB4); //GAMMA RED GROUP2
		for(i=0; i<18; i++)
		{
			SSD2828_W_Data(SigMode,channel,NT37701A_bufferG[i+18]);	
		}									 
		SSD2828_W_Reg(SigMode,channel,0xBC,15); 
		SSD2828_W_Cmd(SigMode,channel,0xBF);	
		SSD2828_W_Data(SigMode,channel,0xB5); //GAMMA RED GROUP3
		for(i=0; i<14; i++)
		{
			SSD2828_W_Data(SigMode,channel,NT37701A_bufferG[i+36]);	
		}
		delay_ms(2); 		
//----------------------------------------------------------------------		
		SSD2828_W_Reg(SigMode,channel,0xBC,19); 
		SSD2828_W_Cmd(SigMode,channel,0xBF);	
		SSD2828_W_Data(SigMode,channel,0xB6); //GAMMA RED GROUP1
		for(i=0; i<18; i++)
		{
			SSD2828_W_Data(SigMode,channel,NT37701A_bufferB[i]);	
		}				 	
		SSD2828_W_Reg(SigMode,channel,0xBC,19); 
		SSD2828_W_Cmd(SigMode,channel,0xBF);	
		SSD2828_W_Data(SigMode,channel,0xB7); //GAMMA RED GROUP2
		for(i=0; i<18; i++)
		{
			SSD2828_W_Data(SigMode,channel,NT37701A_bufferB[i+18]);	
		}									 
		SSD2828_W_Reg(SigMode,channel,0xBC,15); 
		SSD2828_W_Cmd(SigMode,channel,0xBF);	
		SSD2828_W_Data(SigMode,channel,0xB8); //GAMMA RED GROUP3
		for(i=0; i<14; i++)
		{
			SSD2828_W_Data(SigMode,channel,NT37701A_bufferB[i+36]);	
		}
		delay_ms(2); 	
	//---------------------------------------------------------			
	  buffer1[0] = 0x02;        
    buffer1[1] = 0xCE;
    buffer1[2] = 0x01;
    SSD2828_W_Array(SigMode,channel,buffer1,0);		
		delay_ms(10); 		
		
	  buffer1[0] = 0x02;        
    buffer1[1] = 0xCC;
    buffer1[2] = 0x00;
    SSD2828_W_Array(SigMode,channel,buffer1,0);
		delay_ms(2); 	
	  buffer1[0] = 0x02;        
    buffer1[1] = 0xCE;
    buffer1[2] = 0x00;
    SSD2828_W_Array(SigMode,channel,buffer1,0);				
		delay_ms(2); 		
		
		SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);				
		delay_ms(2); 
    buffer[0]=NT37701A;	//NT37701A
    buffer[1]=0x03;		
    buffer[4] = Uart_Error_None;
    buffer[5] = 0;
    STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+3);            //返回 写寄存器状态  ：ok   
}

void NT37701A_Video_Read_Gamma(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
			u8 buffer1[7];
			u16 tmp;
			u8 i,j;
			u8 PAGE2[7]={0x06,0xf0,0x55,0xaa,0x52,0x08,0x02};	

			SSD2828_W_Reg(SigMode,channel,0xB7,0x035B);
	    delay_ms(15);	
			SSD2828_W_Array(SigMode,channel,PAGE2,0); 
			delay_ms(5);	    
	
			buffer1[0] = 0x02;  	
			buffer1[1] = 0xBF;
			buffer1[2] = buffer[4]; //gamma_group_sel
			SSD2828_W_Array(SigMode,channel,buffer1,0);		        
	    SSD2828_W_Reg(SigMode,channel,0xB7,0x07C0);
			delay_ms(5);	
	    SSD2828_W_Reg(SigMode,channel,0xC1,0x0012); //返回个数设置	
			SSD2828_W_Reg(SigMode,channel,0xC0,0x0001); //reset	
	    delay_ms(2);			 
			buffer1[0] = 0x01; //read reg
			if(buffer[3]==0x00)
					buffer1[1] = 0xB0; //GAMMA RED
			else if(buffer[3]==0x42)
					buffer1[1] = 0xB3; //GAMMA GREEN
			else if(buffer[3]==0x81)
					buffer1[1] = 0xB6; //GAMMA BLUE				
      SSD2828_W_Array(SigMode,channel,buffer1,0);		 
	    delay_ms(2);  
			
			SSD2828_W_Reg(SigMode,channel,0xD4,0x00FA);
		  SSD2828_W_Cmd(SigMode,channel,0xFF);
			
		  delay_ms(2);		
			for(i=0;i<18;i++)
		  {
					SSD2828_W_Cmd(SigMode,channel,0xFA);		
					tmp=SPI3_Read_u16_Data(channel);
					if(buffer[3]==0x00)
					{				
							NT37701A_bufferR[i]=tmp>>8;
							NT37701A_bufferR[i+1]=tmp;
					}
					else if(buffer[3]==0x42)
					{
							NT37701A_bufferG[i]=tmp>>8;
							NT37701A_bufferG[i+1]=tmp;			
					}
					else if(buffer[3]==0x81)
					{
							NT37701A_bufferB[i]=tmp>>8;
							NT37701A_bufferB[i+1]=tmp;						
					}				
					delay_ms(5);
					i++;				
		  }			
			
	//-----------------------------------------------------------------		
			SSD2828_W_Reg(SigMode,channel,0xB7,0x035B);
	    delay_ms(15);	
			buffer1[0] = 0x02;  	
			buffer1[1] = 0xBF;
			buffer1[2] = buffer[4]; //gamma_group_sel
			SSD2828_W_Array(SigMode,channel,buffer1,0);		        
	    SSD2828_W_Reg(SigMode,channel,0xB7,0x07C0);
			delay_ms(5);	
	    SSD2828_W_Reg(SigMode,channel,0xC1,0x0012); //返回个数设置	
			SSD2828_W_Reg(SigMode,channel,0xC0,0x0001); //reset	
	    delay_ms(2);			 
			buffer1[0] = 0x01; //read reg      
			if(buffer[3]==0x00)
					buffer1[1] = 0xB1; //GAMMA RED
			else if(buffer[3]==0x42)
					buffer1[1] = 0xB4; //GAMMA GREEN
			else if(buffer[3]==0x81)
					buffer1[1] = 0xB7; //GAMMA BLUE		         
      SSD2828_W_Array(SigMode,channel,buffer1,0);		 
	    delay_ms(2);  
			
			SSD2828_W_Reg(SigMode,channel,0xD4,0x00FA);
		  SSD2828_W_Cmd(SigMode,channel,0xFF);
		  delay_ms(2);	
			for(i=0;i<18;i++)
		  {
					SSD2828_W_Cmd(SigMode,channel,0xFA);		
					tmp=SPI3_Read_u16_Data(channel);
					if(buffer[3]==0x00)
					{					
						NT37701A_bufferR[i+18]=tmp>>8;
						NT37701A_bufferR[i+19]=tmp;
					}
					else if(buffer[3]==0x42)
					{
						NT37701A_bufferG[i+18]=tmp>>8;
						NT37701A_bufferG[i+19]=tmp;				
					}
					else if(buffer[3]==0x81)
					{
						NT37701A_bufferB[i+18]=tmp>>8;
						NT37701A_bufferB[i+19]=tmp;				
					}					
			    delay_ms(5);
				  i++;			
		  }			
	//-----------------------------------------------------------------		
			SSD2828_W_Reg(SigMode,channel,0xB7,0x035B);
	    delay_ms(15);	
			buffer1[0] = 0x02;  	
			buffer1[1] = 0xBF;
			buffer1[2] = buffer[4]; //gamma_group_sel
			SSD2828_W_Array(SigMode,channel,buffer1,0);		        
	    SSD2828_W_Reg(SigMode,channel,0xB7,0x07C0);
			delay_ms(5);	
	    SSD2828_W_Reg(SigMode,channel,0xC1,0x0012); //返回个数设置	
			SSD2828_W_Reg(SigMode,channel,0xC0,0x0001); //reset	
	    delay_ms(2);				 
			buffer1[0] = 0x01;        
			if(buffer[3]==0x00)
					buffer1[1] = 0xB2; //GAMMA RED
			else if(buffer[3]==0x42)
					buffer1[1] = 0xB5; //GAMMA GREEN
			else if(buffer[3]==0x81)
					buffer1[1] = 0xB8; //GAMMA BLUE		         
      SSD2828_W_Array(SigMode,channel,buffer1,0);		 
	    delay_ms(2);   
			
			SSD2828_W_Reg(SigMode,channel,0xD4,0x00FA);
		  SSD2828_W_Cmd(SigMode,channel,0xFF);
		  delay_ms(2);
			for(i=0;i<18;i++)
		  {
					SSD2828_W_Cmd(SigMode,channel,0xFA);		
					tmp=SPI3_Read_u16_Data(channel);
					if(buffer[3]==0x00)
					{	
						NT37701A_bufferR[i+36]=tmp>>8;
						NT37701A_bufferR[i+37]=tmp;
					}
					else if(buffer[3]==0x42)
					{	
						NT37701A_bufferG[i+36]=tmp>>8;
						NT37701A_bufferG[i+37]=tmp;
					}	
					else if(buffer[3]==0x81)
					{	
						NT37701A_bufferB[i+36]=tmp>>8;
						NT37701A_bufferB[i+37]=tmp;
					}						
			    delay_ms(5);
				  i++;			
		  }		
			for(j=0;j<50;j++)
			{
				if(buffer[3]==0x00)				
					buffer[4+j]=NT37701A_bufferR[j];	
				else if(buffer[3]==0x42)	
					buffer[4+j]=NT37701A_bufferG[j];	
				else if(buffer[3]==0x81)
					buffer[4+j]=NT37701A_bufferB[j];	
			}			
    buffer[0]=NT37701A;	//NT37701A
    buffer[1]=0x09;
		
		delay_ms(15);
    STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+3);          //返回 读取的 Gamma数据  ：ok  		
    SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
}


void NT37701A_MODE_Switch(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
			u8 buffer1[7];
			u16 tmp;
			u8 i,j;
			u8 PAGE2[7]={0x06,0xf0,0x55,0xaa,0x52,0x08,0x02};		
			SSD2828_W_Reg(SigMode,channel,0xB7,0x035B);  //HS_DCS_LONG_Write
	    delay_ms(15);	
			SSD2828_W_Array(SigMode,channel,PAGE2,0); 
			delay_ms(5);	    
	
			buffer1[0] = 0x02;  	
			buffer1[1] = 0xBF;
			buffer1[2] = buffer[4]; //gamma_group_sel
			SSD2828_W_Array(SigMode,channel,buffer1,0);		
			SSD2828_W_Reg(SigMode,channel,0xB7,0x07C0); //LP_DCS_LONG_Read  
			delay_ms(5);	
}

void NT37701A_Command_Read_Gamma(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
			u8 buffer1[7];
			u16 tmp;
			u8 i,j;
			
			u8 PAGE2[7]={0x06,0xf0,0x55,0xaa,0x52,0x08,0x02};		
			SSD2828_W_Reg(SigMode,channel,0xB7,0x0740);  //LP_DCS_LONG_Write
	    delay_ms(15);	
			SSD2828_W_Array(SigMode,channel,PAGE2,0); 
			delay_ms(5);	    
	
			buffer1[0] = 0x02;  	
			buffer1[1] = 0xBF;
			buffer1[2] = buffer[4]; //gamma_group_sel
			SSD2828_W_Array(SigMode,channel,buffer1,0);		
			SSD2828_W_Reg(SigMode,channel,0xB7,0x07DB); //HS_DCS_LONG_Read  
			Generate_MIPI_Read(pdev,SigMode,channel,0xB0 ,18, NT37701A_bufferR);delay_ms(15); //预读取，触发回读流程
			
			delay_ms(5);				
			if(buffer[3]==0x00)	
			{
				NT37701A_MODE_Switch(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data);
				Generate_MIPI_Read(pdev,SigMode,channel,0xB0 ,18, NT37701A_bufferR);delay_ms(15);
				NT37701A_MODE_Switch(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data);
				Generate_MIPI_Read(pdev,SigMode,channel,0xB1 ,18, NT37701A_bufferR+18);	delay_ms(15);
				NT37701A_MODE_Switch(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data);
				Generate_MIPI_Read(pdev,SigMode,channel,0xB2 ,14, NT37701A_bufferR+18+18);delay_ms(15);
			}
			else if(buffer[3]==0x42)	
			{
				NT37701A_MODE_Switch(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data);
				Generate_MIPI_Read(pdev,SigMode,channel,0xB3 ,18, NT37701A_bufferG);delay_ms(15);
				NT37701A_MODE_Switch(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data);
				Generate_MIPI_Read(pdev,SigMode,channel,0xB4 ,18, NT37701A_bufferG+18);	delay_ms(15);
				NT37701A_MODE_Switch(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data);
				Generate_MIPI_Read(pdev,SigMode,channel,0xB5 ,14, NT37701A_bufferG+18+18);delay_ms(15);
			}
			else if(buffer[3]==0x81)
			{
				NT37701A_MODE_Switch(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data);
				Generate_MIPI_Read(pdev,SigMode,channel,0xB6 ,18, NT37701A_bufferB);delay_ms(15);
				NT37701A_MODE_Switch(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data);
				Generate_MIPI_Read(pdev,SigMode,channel,0xB7 ,18, NT37701A_bufferB+18);	delay_ms(15);
				NT37701A_MODE_Switch(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data);
				Generate_MIPI_Read(pdev,SigMode,channel,0xB8 ,14, NT37701A_bufferB+18+18);delay_ms(15);
			}
			
			for(j=0;j<50;j++)
			{
				if(buffer[3]==0x00)				
					buffer[4+j]=NT37701A_bufferR[j];	
				else if(buffer[3]==0x42)	
					buffer[4+j]=NT37701A_bufferG[j];	
				else if(buffer[3]==0x81)
					buffer[4+j]=NT37701A_bufferB[j];	
			}			
    buffer[0]=NT37701A;	//NT37701A
    buffer[1]=0x09;
		
		delay_ms(15);
    STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+3);          //返回 读取的 Gamma数据  ：ok  		
    SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
}



void NT37701A_Read_Gamma(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
		if(SigMode==Video_Mode)
		{
			NT37701A_Video_Read_Gamma(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data);
		}
		else if(SigMode==CMD_Mode)
		{
			NT37701A_Command_Read_Gamma(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data);
		}
}

void NT37701A_Gamma_OTP_Start(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
    u8 buffer1[10];
	  u8 mtp_flag;
		u8 PAGE1[7]={0x06,0xf0,0x55,0xaa,0x52,0x08,0x01};	
		
		u8 OTP_en_off[8]={0x07,0xED,0x00,0x00,0x00,0x00,0x00,0x00};
		u8 OTP_en_PAGE0[8]={0x07,0xED,0x7F,0x00,0x00,0x00,0x00,0x00};
	  u8 OTP_en_PAGE1[8]={0x07,0xED,0x80,0x0F,0x00,0x00,0x00,0x00};
	  u8 OTP_en_PAGE2[9]={0x08,0xED,0x00,0xE0,0x00,0x00,0x00,0x00,0x08};		
	  u8 OTP_en_PAGE3[8]={0x07,0xED,0x00,0x00,0x07,0x00,0x00,0x00};	
	  u8 OTP_en_PAGE4[8]={0x07,0xED,0x00,0x00,0x18,0x00,0x00,0x00};	
	  u8 OTP_en_PAGE5[8]={0x07,0xED,0x00,0x00,0x60,0x00,0x00,0x00};
	  u8 OTP_en_PAGE6[8]={0x07,0xED,0x00,0x00,0x80,0x00,0x00,0x00};		
	  u8 OTP_en_PAGE7[8]={0x07,0xED,0x00,0x00,0x00,0x03,0x00,0x00};		
	  u8 OTP_en_PAGE8[8]={0x07,0xED,0x00,0x00,0x00,0x0C,0x00,0x00};			
		
		u8 OTP_all_PAGE[9]={0x08,0xED,0xFF,0xEF,0xFF,0x0F,0x08,0x00,0x08};
			
	  SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
		delay_ms(15); 
    buffer1[0] = 0x01;
    buffer1[1] = 0x28;                    
    SSD2828_W_Array(SigMode,channel,buffer1,0); 
		delay_ms(10); 
		buffer1[0] = 0x01;
    buffer1[1] = 0x11;  	
    SSD2828_W_Array(SigMode,channel,buffer1,0); 
		delay_ms(120); 	
	  SSD2828_W_Array(SigMode,channel,PAGE1,0);//Enable CMD2 Page 1
		delay_ms(5); 
	  buffer1[0] = 0x03;
    buffer1[1] = 0xc9; 
	  buffer1[2] = 0x0c;
    buffer1[3] = 0x11; 	
    SSD2828_W_Array(SigMode,channel,buffer1,0);//VRGH_MTP[7:0] = 0x0F (6.2V)
		delay_ms(20); 
	  buffer1[0] = 0x04;
    buffer1[1] = 0xEB; 
	  buffer1[2] = 0x87;
    buffer1[3] = 0x78; 
	  buffer1[4] = 0x01;		
    SSD2828_W_Array(SigMode,channel,buffer1,0);//Enable Internal MTP Power 
		delay_ms(60); 	
	  buffer1[0] = 0x02;
    buffer1[1] = 0xCA; 
	  buffer1[2] = 0x01;		
    SSD2828_W_Array(SigMode,channel,buffer1,0); //PDSTB = 1
		delay_ms(20); 
		buffer1[0] = 0x03;
    buffer1[1] = 0xCA; 
	  buffer1[2] = 0x01;	
	  buffer1[3] = 0x02;	//PTM_RB[3:0] = 2 (Program Mode)
		SSD2828_W_Array(SigMode,channel,buffer1,0);
		delay_ms(20); 

	
		if(buffer[3]==0x02)  //otp Gamma
		{	 
			  buffer1[0] = 0x02;        
				buffer1[1] = 0xCC;
				buffer1[2] = 0x30;
				SSD2828_W_Array(SigMode,channel,buffer1,0);		
				delay_ms(1); 		
				buffer1[0] = 0x02;        
				buffer1[1] = 0xCE;
				buffer1[2] = 0x01;
				SSD2828_W_Array(SigMode,channel,buffer1,0);		
				delay_ms(5);			
				SSD2828_W_Array(SigMode,channel,OTP_en_PAGE2,0);	
		}
		else if(buffer[3]==0xA0)  //otp ALL
		{
			SSD2828_W_Array(SigMode,channel,OTP_all_PAGE,0);	
		}
		else if(buffer[3]==0x00)  //otp PAGE 0
		{
 			SSD2828_W_Array(SigMode,channel,OTP_en_PAGE0,0);	
		}
		else if(buffer[3]==0x01)  //otp PAGE 1
		{	 
 			SSD2828_W_Array(SigMode,channel,OTP_en_PAGE1,0);	
		}
		else if(buffer[3]==0x03)  //otp PAGE 5
		{
  		SSD2828_W_Array(SigMode,channel,OTP_en_PAGE3,0);	
		}
		else if(buffer[3]==0x04)  //otp PAGE 4
		{		 
			SSD2828_W_Array(SigMode,channel,OTP_en_PAGE4,0);	
		}
		else if(buffer[3]==0x05)  //otp PAGE 5
		{
  		SSD2828_W_Array(SigMode,channel,OTP_en_PAGE5,0);	
		}
		else if(buffer[3]==0x06)  //otp PAGE 6
		{
   		SSD2828_W_Array(SigMode,channel,OTP_en_PAGE6,0);	
		}		
		else if(buffer[3]==0x07)  //otp PAGE 7
		{
   		SSD2828_W_Array(SigMode,channel,OTP_en_PAGE7,0);	
		}		
		else if(buffer[3]==0x08)  //otp PAGE 8
		{
   		SSD2828_W_Array(SigMode,channel,OTP_en_PAGE8,0);	
		}				
		delay_ms(50); 	 
		SSD2828_W_Array(SigMode,channel,PAGE1,0);//Enable CMD2 Page 1
		delay_ms(2);		
    buffer1[0] = 0x04;
    buffer1[1] = 0xEE;
    buffer1[2] = 0xA5;    
	  buffer1[3] = 0x5A;    
	  buffer1[4] = 0x3C;    
		SSD2828_W_Array(SigMode,channel,buffer1,0);			
		delay_ms(5000);//	x must be large than 3s if need program all page
	//	SSD2828_W_Array(SigMode,channel,OTP_en_off,0);		
//		buffer[1] = 0x0E;
		buffer[2] = 0x02;
		buffer[3] = Uart_Error_None;   //返回 OTP Gamma数据  ：OK
		buffer[4] = 0;
		STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+3);         
}


void ProcessForIc6A( USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8 buffer[],u16 LP_B7_Data,u16 HS_B7_Data)
{	
	switch(buffer[1])
	{
    case 0x01:                                      //写寄存器
         NT37701A_Write_Register(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data);break;	
    case 0x02:                                      //读寄存器
         NT37701A_Read_Register(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data);break;	
    case 0x03:                                      //自动gamma写
         NT37701A_Gamma_Update(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data);break;			
    case 0x08:                                      //写Gamma数据    
         NT37701A_Write_Gamma(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data);break;
    case 0x09:                                      //读Gamma数据
         NT37701A_Read_Gamma(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data);break; 
		case 0x0D://OTP Gamma
         NT37701A_Gamma_OTP_Start(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data);  break;

    default:	break;	
	}	
}


